package com.kingsoft.miot.bridge.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingsoft.miot.bridge.utils.ValidationUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.kingsoft.miot.bridge.common.CommonResult;
import com.kingsoft.miot.bridge.controller.resp.SceneInfoResp;
import com.kingsoft.miot.bridge.pojo.scene.SceneInfo;
import com.kingsoft.miot.bridge.service.SceneInfoService;

/**
 * 
 * @author melody
 *
 */
@RestController
public class SceneController {

	private static final Logger LOGGER = LoggerFactory.getLogger(SceneController.class);

	@Autowired
	private SceneInfoService sceneInfoService;

//	@Autowired
//	private SceneHouseRecordService sceneHouseRecordService;

	/**
	 * 通过酒店和房间id获取场景列表
	 * 
	 * @param id
	 * @param house_id
	 * @return
	 */
	@RequestMapping(value = "data/hotels/{id}/house/{house_id}/scene", method = RequestMethod.GET, produces = "text/html; charset=UTF-8")
	public String getSceneListByHotelAndHouse(@PathVariable String id, @PathVariable String house_id) {
		// 1. 如果有数据为空
		if (StringUtils.isAnyBlank((new String[] { id, house_id }))) {
			return CommonResult.returnJson(false, 400, "校验数据为空");
		}
		try {
			// 2. 请求数据
			List<SceneInfo> sceneList = null;
			sceneList = sceneInfoService.getByHotelIdAndHouseId(Integer.valueOf(id), Integer.valueOf(house_id));
			if (sceneList == null || sceneList.size() <= 0) {
				return CommonResult.returnJson(false, 400, "返回的数据为空");
			}
			List<SceneInfoResp> retData = new ArrayList<SceneInfoResp>();
			// 3. 构造返回数据
			for (int i = 0; i < sceneList.size(); i++) {
				SceneInfo sceneInfo = sceneList.get(i);
				SceneInfoResp resp = new SceneInfoResp();
				// 返回字符串
				resp.setId(sceneInfo.getScene_info_id() + "");
				resp.setName(sceneInfo.getScene_info_name());
				Map<String, Object> status = new HashMap<>();
				if (sceneInfo.getScene_info_status() == 1) {
					status.put("name", "开");
					status.put("value", 1);
				} else {
					status.put("name", "关");
					status.put("value", 0);
				}
				resp.setStatus(status);
				resp.setInfo(JSON.parseObject(sceneInfo.getScene_info_opration_list(), List.class));
				retData.add(resp);
			}
			return JSON.toJSONString(retData);
		} catch (Exception e) {
			LOGGER.error("获取房间场景列表失败。");
			return CommonResult.returnJson(false, 500, "获取房间场景列表失败");
		}
	}
	@RequestMapping(value = "data/hotels/scene/create", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	public CompletableFuture<String>  createScene(@RequestBody JSONObject params) {
		return CompletableFuture.supplyAsync(() -> {
			int count = sceneInfoService.insertOne(params);
			return CommonResult.jsonString(count);
		});
	}


	/**
	 * 房间管理-房间详情-场景-更新场景状态
	 * 请求方式：POST
	 *
	 * @return
	 */
	@RequestMapping(value = "/data/hotels/{id}/house/{house_id}/scene/{scene_id}/update_status",
			produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
	public CompletableFuture<String> updateStatus(@RequestBody JSONObject params, @PathVariable int id, @PathVariable int house_id, @PathVariable int scene_id) {
		LOGGER.info("结果是" + params + id + house_id + scene_id);
		JSONObject resultJson = new JSONObject();
		resultJson.put("status", 1);
		return CompletableFuture.supplyAsync(() -> {
			return CommonResult.jsonString(resultJson);
		});
	}

	/**
	 * 房间管理-房间详情-场景-获取场景列表
	 * 请求方式：GET
	 *
	 * @return
	 */
	@RequestMapping(value = "/data/hotels/{id}/house/{house_id}/scene",
			produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
	public CompletableFuture<String> findSceneList(@PathVariable int id, @PathVariable int house_id) {
		LOGGER.info("结果是" + id + house_id);
		JSONArray resultArray = new JSONArray();
		for (int i = 0; i < 10; i++) {
			JSONObject start = new JSONObject();
			start.put("id", 1+i);
			start.put("name", "入住");
			start.put("status", ValidationUtils.getDeviceStatus(1));
			JSONArray infoJson = new JSONArray();
			JSONArray data1 = new JSONArray();
			JSONObject jsonName1 = new JSONObject();
			data1.add("门厅吸顶灯");
			data1.add("阳台吸顶灯");
			data1.add("客厅吸顶灯");
			jsonName1.put("name", "开启吸顶灯");
			jsonName1.put("data", data1);
			JSONObject jsonName2 = new JSONObject();
			JSONArray data2 = new JSONArray();
			data2.add("客户窗帘");
			data2.add("卧室窗帘");
			jsonName2.put("name", "打开窗帘");
			jsonName2.put("data", data2);
			infoJson.add(jsonName1);
			infoJson.add(jsonName2);
//		info.put("info", infoJson);
			start.put("info",infoJson);
			resultArray.add(start);
		}
		return CompletableFuture.supplyAsync(() -> {
			return CommonResult.jsonString(resultArray);
		});
	}

}
